package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Foodadditive;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FoodadditiveDAO")
@Transactional
public class FoodadditiveDAOImpl extends AbstractJpaDao<Foodadditive> implements FoodadditiveDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodadditive.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FoodadditiveDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodadditive findFoodadditiveById(Integer id) throws DataAccessException {

		return findFoodadditiveById(id, -1, -1);
	}

	@Transactional
	public Foodadditive findFoodadditiveById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodadditiveById", startResult, maxRows, id);
			return (Foodadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodadditive> findFoodadditiveByName(String name) throws DataAccessException {

		return findFoodadditiveByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodadditive> findFoodadditiveByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodadditiveByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodadditive> findFoodadditiveByNameContaining(String name) throws DataAccessException {

		return findFoodadditiveByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodadditive> findFoodadditiveByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodadditiveByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodadditive> findFoodadditiveByContent(BigDecimal content) throws DataAccessException {

		return findFoodadditiveByContent(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodadditive> findFoodadditiveByContent(BigDecimal content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodadditiveByContent", startResult, maxRows, content);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodadditive> findFoodadditiveByDescription(String description) throws DataAccessException {

		return findFoodadditiveByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodadditive> findFoodadditiveByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodadditiveByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodadditive> findFoodadditiveByDescriptionContaining(String description) throws DataAccessException {

		return findFoodadditiveByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodadditive> findFoodadditiveByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodadditiveByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@Transactional
	public Foodadditive findFoodadditiveByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodadditiveByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodadditive findFoodadditiveByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodadditiveByPrimaryKey", startResult, maxRows, id);
			return (Foodadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodadditive> findFoodadditivesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodadditivesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodadditive> findAllFoodadditives() throws DataAccessException {

		return findAllFoodadditives(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodadditive> findAllFoodadditives(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodadditives", startResult, maxRows);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodadditive> findFoodadditivesByAdditive(Integer additiveId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodadditivesByAdditive", start, max, additiveId);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodadditive> findFoodadditivesByFood(Integer foodId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodadditivesByFood", start, max, foodId);
		return new LinkedHashSet<Foodadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodadditive> findFoodadditiveByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Foodadditive>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodadditive> findFoodadditivesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodadditive.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Foodadditive> foodadditives = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodadditives;
	}

	@Transactional
	public Foodadditive findFoodadditiveByPhysicalPrimaryKey(Integer foodId, Integer additiveId, BigDecimal content)
			throws DataAccessException {
		return findFoodadditiveByPhysicalPrimaryKey(foodId, additiveId, content, -1, -1);
	}

	@Transactional
	public Foodadditive findFoodadditiveByPhysicalPrimaryKey(Integer foodId, Integer additiveId, BigDecimal content,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodadditiveByPhyscialPrimaryKey", startResult, maxRows, foodId,
					additiveId, content);
			return (Foodadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodadditive entity) {
		return true;
	}
}
